//
//  KZVocabularyImproveLookFailuresVC.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/9/6.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZVocabularyImproveLookFailuresVC.h"

// model
#import "KZVocabularyPackageModel.h"

// view
#import "KZVocabularyImproveTestReviewView.h"
#import "KZIndexProgressView.h"     // 索引进度



@interface KZVocabularyImproveLookFailuresVC () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) KZIndexProgressView *indexView;
@end

@implementation KZVocabularyImproveLookFailuresVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"不合格词汇";
    [self addLeftBackBarButtonItem];
    
    [self setupUI];
}

- (void)setupUI {
    
    _indexView = [KZIndexProgressView new];
    _indexView.totalIndex = _failureModels.count;
    _indexView.index = 1;
    [self.view addSubview:_indexView];
    [_indexView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.equalTo(@40);
    }];
    
    _scroll = [UIScrollView new];
    _scroll.layer.masksToBounds = false;
    _scroll.pagingEnabled = true;
    _scroll.delegate = self;
    [self.view addSubview:_scroll];
    [_scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(_indexView.mas_bottom);
        make.width.equalTo(self.view);
        if (@available(ios 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(@0);
        }
    }];
    
    KZVocabularyImproveTestReviewView *last = nil;
    for (int i = 0; i < _failureModels.count; i ++) {
        KZVocabularyImproveTestReviewView *view = [KZVocabularyImproveTestReviewView new];
        view.model = _failureModels[i];
        [_scroll addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.height.width.equalTo(_scroll);
            if (last) {
                make.left.equalTo(last.mas_right);
            } else {
                make.left.equalTo(@0);
            }
        }];
        last = view;
    }
    
    [last mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@0);
    }];
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    NSInteger index = (*targetContentOffset).x / scrollView.width;
    _indexView.index = index + 1;
}



@end
